/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.uci.ics.crawler4j.examples.basic;


import java.util.List;
import java.util.regex.Pattern;

import org.htmlcleaner.TagNode;

import com.googlecode.crawlerextractor.crawler4j.crawler.Page;
import com.googlecode.crawlerextractor.crawler4j.crawler.WebCrawler;
import com.googlecode.crawlerextractor.crawler4j.parser.HtmlParseData;
import com.googlecode.crawlerextractor.crawler4j.url.WebURL;

/**
 * @author Yasser Ganjisaffar <lastname at gmail dot com>
 */
public class BasicLocalCrawler extends WebCrawler {

	private final static Pattern FILTERS = Pattern.compile(".*(\\.(css|js|bmp|gif|jpe?g" + "|png|tiff?|mid|mp2|mp3|mp4"
			+ "|wav|avi|mov|mpeg|ram|m4v|pdf" + "|rm|smil|wmv|swf|wma|zip|rar|gz))$");

	@Override
	public boolean shouldScan(final WebURL url) {
		if (!this.shouldVisit( url) ) {
			return false;
		}
		final String href = url.getURL().toLowerCase();
		return !href.contains("appdev");
	}
	/**
	 * You should implement this function to specify whether the given url
	 * should be crawled or not (based on your crawling logic).
	 */
	@Override
	public boolean shouldVisit(final WebURL url) {
		final String href = url.getURL().toLowerCase();
		return !FILTERS.matcher(href).matches() && href.startsWith("http://localhost/tomcat/");
	}

	/**
	 * This function is called when a page is fetched and ready to be processed
	 * by your program.
	 */
	@Override
	public void visit(final Page page) {
		final int docid = page.getWebURL().getDocid();
		final String url = page.getWebURL().getURL();
		final String domain = page.getWebURL().getDomain();
		final String path = page.getWebURL().getPath();
		final String subDomain = page.getWebURL().getSubDomain();
		final String parentUrl = page.getWebURL().getParentUrl();

		System.out.print("Docid: " + docid);
		System.out.print(" | URL: " + url);
		System.out.print(" | Domain: '" + domain + "'");
		System.out.print(" | Sub-domain: '" + subDomain + "'");
		System.out.print(" | Path: '" + path + "'");
		System.out.print(" | Parent page: " + parentUrl);

		if (page.getParseData() instanceof HtmlParseData) {
			final HtmlParseData htmlParseData = (HtmlParseData) page.getParseData();
			final String text = htmlParseData.getText();
			final String html = htmlParseData.getHtml();
			final List<WebURL> links = htmlParseData.getOutgoingUrls();
			final TagNode node=htmlParseData.getCleanedHtml();
			System.err.print("getCleanedHtml().getAttributeByName('title'): " +((TagNode) node.getElementListByName("title", true).get(0)).getText());
			System.out.print("Text length: " + text.length());
			System.out.print("Html length: " + html.length());
			System.out.print("Number of outgoing links: " + links.size());
		}
		System.out.println("");
	}
}
